clear all
set more off
*capture log close
set logtype text
set matsize 10000

local mydate: di %tdDNCY date(c(current_date), "DMY")

global output 		"O:\returns"
global data_path 	"W:\returns\REStat_estimation_data" 
global work			"W:\returns\REStat_estimation_data" 
global graphs 		"O:\returns"

global graphs_do	0
* choose 0 =  no graphs; 1 = do graphs
global horse_race	1
global dummy		1  /* 1 if estimate constant /  interacted TE models, 0 otherwise */
global ownfirm		0  /* 1 if only use obs where the individuals in the firm they were in at t = 0 */
*log using "${output}\returns_EST_CEM_`mydate'_noIQ_stem.log" , replace

**************************************************************************************

* This file produces the results using STEM individuals only
* Aghion, Akcigit, Hyytinen & Toivanen : 
* A year older, a year wiser (and farther from the frontier):invention rents and human capital depreciation


**************************************************************************************

/*  
\
()
{}
[]
=
`i'
 >
 <
 |
 +
*/

* set seed 
set seed 346352014

**********************************************************************************************

* inventors

**********************************************************************************************

* NOTE on data cem - matching vector
* A: cem on educ, STEM, IQ, manuf, sme, region
* B: add age
* C: add dwage over past 3 years
* D: add avg wage over past 4 years
* E: educ, STEM, IQ, manuf, region, age, firm size quintiles 
* F: educ, STEM, manuf, region, age, firm size quintiles 
* G: educ, STEM, manuf, region, age, firm size quintiles, dtf, suorv_d

capture program drop alterations
program define alterations, eclass
		matrix A = e(b)
		matrix A[1,96] = 0
		ereturn repost b = A
		ereturn repost V = koev
end



* choose data
* NOTE: choose data_int == 6 if you want to use the IQ data; use data_int == 5 if you want to use the large data
global data 	G
global data_int 5

global sose_types inventors sose_entr whitec sose_bc 
global cow_types_oth sose_sr sose_mngt_jr sose_jr
	
local datacounter 		= 3
forvalues datacounter 	= 3/4  {

	if  `datacounter' == 1 {
		use "${data_path}\cem_estim_inventors$data.dta", clear
		local sose_type 	= "inv"
		}
	else if `datacounter' == 2 {
		use "${data_path}\cem_estim_sose_entr$data.dta", clear
		local sose_type 	= "entr"
		}
	else if `datacounter' == 3 {
		use "${data_path}\returns_EST_sose_mngt_sr$data.dta", clear
		foreach k of global cow_types_oth {
			append using "${data_path}\returns_EST_`k'$data.dta"
			}
		local sose_type 	= "whitec"
		}
	else if `datacounter' == 4 {
		use "${data_path}\returns_EST_sose_bc$data.dta", clear
		local sose_type 	= "bluec"
		}

	drop if ika >= 60
	
	capture: 	drop if wage_l14_obs != 1
	capture:	rename inventor treated
	capture:	gen treated_cow = inv_1
	capture: 	gen	cem_weights = 1
	capture: 	drop treated
	replace treated_cow			= 0 		if treated_cow == .  /* corresponds to inv_1 */
	capture: bysort shnro: egen treated	= max(treated_cow)
	* regressions
	gen lnwage					= ln(wage)
	gen lnearnings				= ln(earnings)
	gen lnwage_alt				= ln(wage + 1)
	gen lnearnings_alt			= ln(earnings + 1)

	gen cem_year0 				= .
	replace cem_year0			= vuosi if cem_matched == 1
	bysort shnro: egen cem_year	= max(cem_year0)

	* NOTE: if cem_year == ., the individual is an inventor who was not matched by cem

	sort shnro vuosi
	forvalues i = 0/10 {
		gen l_`i' 				= .
		replace l_`i' 			= 0  
		replace l_`i' 			= 1 if vuosi == cem_year + `i' 
		}
		
	forvalues i = 1/10 {
		gen inv_l_`i' 			= .
		replace inv_l_`i' 		= 0  
		replace inv_l_`i' 		= 1 if vuosi == cem_year + `i' & treated == 1 
		}
		
	forvalues i = 1/4 {
		gen f_`i' 				= .
		replace f_`i' 			= 0 
		replace f_`i' 			= 1 if vuosi == cem_year - `i'
		gen inv_f_`i' 			= .
		replace inv_f_`i' 		= 0 
		replace inv_f_`i' 		= 1 if vuosi == cem_year - `i' & treated == 1 
		}
	forvalues i = 5/7 {
		gen fl_`i' 				= .
		replace fl_`i' 			= 0 
		replace fl_`i' 			= 1 if vuosi == cem_year - `i' 
		gen inv_fl_`i' 			= .
		replace inv_fl_`i' 		= 0 
		replace inv_fl_`i' 		= 1 if vuosi == cem_year - `i' & treated == 1 
		}	
		
		
	gen treatment_time			= vuosi - cem_year	
	replace treatment_time		= . if (treatment_time < -6 | treatment_time > 10) & treatment_time != .

	egen minage					= min(ika)
	gen age2					= ika
	replace age2				= minage + 1 if age2 <= minage + 1
	local minage_plus			= minage + 1
	display "`minage_plus'"
							
	char vuosi[omit] 1994
	char age2[omit] `minage_plus'
		
	xi i.vuosi*i.age2

	global calendar_year_ds _Ivuosi_*
	global age_ds _Iage2_*
	global interactions _IvuoXage*

	gen after					= 0
	replace after				= 1		if treatment_time >= 0 & treatment_time != .
	gen treated_post			= treated * after

	if $graphs_do == 1 {	
		regr lnwage  				$calendar_year_ds $age_ds $interactions if cem_year != . 
		predict lnwage_hat  
		regr lnearnings  			$calendar_year_ds $age_ds $interactions if cem_year != . 
		predict lnearnings_hat

		gen wage_hat			= exp(lnwage_hat)
		gen earnings_hat		= exp(lnearnings_hat)
		
		bysort treatment_time: egen wage_avg_control_raw0	= mean(wage) if treated == 0 & cem_year != . 
		bysort treatment_time: egen wage_avg_treat_raw0		= mean(wage) if treated == 1 & cem_year != .
		
		bysort treatment_time: egen wage_avg_control0		= mean(wage_hat) if treated == 0 & cem_year != . 
		bysort treatment_time: egen wage_avg_treat0			= mean(wage_hat) if treated == 1 & cem_year != .

		bysort treatment_time: egen earn_avg_control0		= mean(earnings_hat) if treated == 0 & cem_year != . 
		bysort treatment_time: egen earn_avg_treat0			= mean(earnings_hat) if treated == 1 & cem_year != .

		bysort treatment_time: egen wage_avg_control_raw	= mean(wage_avg_control_raw0)  
		bysort treatment_time: egen wage_avg_treat_raw		= mean(wage_avg_treat_raw0) 
		
		bysort treatment_time: egen wage_avg_control		= mean(wage_avg_control0)  
		bysort treatment_time: egen wage_avg_treat			= mean(wage_avg_treat0) 

		bysort treatment_time: egen earn_avg_control		= mean(earn_avg_control0) 
		bysort treatment_time: egen earn_avg_treat			= mean(earn_avg_treat0) 

		bysort vuosi: gen win_vuosi_ind						= _n if cem_year != .
		bysort treatment_time: gen win_tyear_ind			= _n

		twoway 	line wage_avg_treat_raw treatment_time if win_tyear_ind == 1 || ///
			line wage_avg_control_raw treatment_time if win_tyear_ind == 1, ///
			xline(0) ///
			legend(lab(1 "treated coworker") lab(2 "controls") rows(1)) ///
			graphregion(color(white)) bgcolor(white)
		graph export "${graphs}\inventor_wage_raw_`sose_type'$data.pdf" , replace
		
		twoway 	line wage_avg_treat treatment_time if win_tyear_ind == 1 || ///
				line wage_avg_control treatment_time if win_tyear_ind == 1, ///
				xline(0) ///
				legend(lab(1 "treated coworker") lab(2 "controls") rows(1)) ///
				graphregion(color(white)) bgcolor(white)
			graph export "${graphs}\wage_time_`sose_type'$data.pdf" , replace
				
		twoway 	line earn_avg_treat treatment_time if win_tyear_ind == 1 || ///
				line earn_avg_control treatment_time if win_tyear_ind == 1, ///
				xline(0) ///
				legend(lab(1 "treated coworker") lab(2 "controls") rows(1)) ///
				graphregion(color(white)) bgcolor(white)
			graph export "${graphs}\earn_time_`sose_type'$data.pdf" , replace

		gen earn_wage			= earnings / wage
		gen earn_wage_hat		= earnings_hat / wage_hat
		display "controls, wage"
		tabstat wage	 					if treated == 0, stat(mean p1 p5 p10 p25 p50 p75 p90 p95 p99 n)	by(after)
		display "treated, wage"
		tabstat wage	 					if treated == 1, stat(mean p1 p5 p10 p25 p50 p75 p90 p95 p99 n)	by(after)
		display "controls, earnings"
		tabstat earnings 					if treated == 0, stat(mean p1 p5 p10 p25 p50 p75 p90 p95 p99 n)	by(after)
		display "treated, earnings"
		tabstat earnings 					if treated == 1, stat(mean p1 p5 p10 p25 p50 p75 p90 p95 p99 n)	by(after)
		tabstat earn_wage 					if earn_wage > 1 & treated == 1, stat(mean p1 p5 p10 p25 p50 p75 p90 p95 p99 n)	by(after)
			
		bysort vuosi: egen earn_wage_control_v0				= mean(earn_wage) if treated == 0 & cem_year != .
		bysort vuosi: egen earn_wage_treat_v0				= mean(earn_wage) if treated == 1 & cem_year != .

		bysort vuosi: egen earn_wage_control_v				= mean(earn_wage_control_v0) 
		bysort vuosi: egen earn_wage_treat_v				= mean(earn_wage_treat_v0)  

		bysort treatment_time: egen earn_wage_control_t0	= mean(earn_wage) if treated == 0 & cem_year != .
		bysort treatment_time: egen earn_wage_treat_t0		= mean(earn_wage) if treated == 1 & cem_year != .

		bysort vuosi: egen earn_wage_control_t				= mean(earn_wage_control_t0)
		bysort vuosi: egen earn_wage_treat_t				= mean(earn_wage_treat_t0) 


		twoway 	scatter earn_wage_treat_v vuosi if win_vuosi_ind == 1 || ///
				scatter earn_wage_control_v vuosi  if win_vuosi_ind == 1 , ///
				legend(lab(1 "inventors") lab(2 "controls") rows(1)) ///
				graphregion(color(white)) bgcolor(white)
			graph export "${graphs}\earn_wage_vuosi_`sose_type'$data.pdf" , replace

		twoway 	scatter earn_wage_treat_t treatment_time if win_vuosi_ind == 1  || ///
				scatter earn_wage_control_t treatment_time if win_vuosi_ind == 1 , ///
				xline(0) ///
				legend(lab(1 "inventors") lab(2 "controls") rows(1)) ///
				graphregion(color(white)) bgcolor(white)
			graph export "${graphs}\earn_wage_ttime_`sose_type'$data.pdf" , replace
			
		twoway 	kdensity wage_hat if treated == 1 & cem_year != . & treatment_time < 0 & wage < 200000 & wage > 20000 || ///	
				kdensity wage_hat if treated == 0 & cem_year != . & treatment_time < 0 & wage < 200000 & wage > 20000 || ///
				kdensity wage_hat if treated == 1 & cem_year != . & treatment_time >= 0 & wage < 200000 & wage > 20000 || ///	
				kdensity wage_hat if treated == 0 & cem_year != . & treatment_time >= 0 & wage < 200000 & wage > 20000 , ///
				legend(lab(1 "treated before") lab(2 "controls before") lab(3 "treated after") lab(4 "controls after") rows(2)) ///
				xtitle("wage, 2014 euros") ytitle("density") ///
				graphregion(color(white)) bgcolor(white)
			graph export "${graphs}\distr_wage_ttime_`sose_type'$data.pdf" , replace
				
		twoway 	kdensity earnings_hat if treated == 1 & cem_year != . & treatment_time < 0 & earnings < 200000 & earnings > 20000 || ///	
				kdensity earnings_hat if treated == 0 & cem_year != . & treatment_time < 0 & earnings < 200000 & earnings > 20000 || ///
				kdensity earnings_hat if treated == 1 & cem_year != . & treatment_time >= 0 & earnings < 200000 & earnings > 20000 || ///	
				kdensity earnings_hat if treated == 0 & cem_year != . & treatment_time >= 0 & earnings < 200000 & earnings > 20000 , ///
				legend(lab(1 "treated before") lab(2 "controls before") lab(3 "treated after") lab(4 "controls after") rows(2)) ///
				xtitle("earnings, 2014 euros") ytitle("density") ///
				graphregion(color(white)) bgcolor(white)
			graph export "${graphs}\distr_earnings_ttime_`sose_type'$data.pdf" , replace
		}
			
	* NOTE: using shnro or shtun makes no difference

	egen shnro_real				= group(shnro)
	xtset shnro_real
	
	
	replace firm_size			= firm_size / 1000
	gen firm_size2				= firm_size^2
	gen firm_size3				= firm_size^3


	bysort shnro: egen total_firm_size	= total(firm_size)
	
	gen ue_d					= .
	replace ue_d				= 0 			if ptoim1 == "11"  & sykstun != ""					/* 11 = työllinen */
	replace ue_d				= 1 			if ptoim1 != "11"
	sort shnro vuosi
	gen firm_size_new			= firm_size
	replace firm_size_new = firm_size_new[_n - 1]		if firm_size_new[_n] == . & firm_size_new[_n - 1] != . & ue_d[_n] == 1


	bysort shnro: egen fam_size_max		= max(family_size_max)
	replace fam_size_max				= 0 if fam_size_max == .

	gen treated_famsize			= treated_post * fam_size_max

	if  $data_int == 6 {
		gen high_iq					= .
		replace high_iq				= 0 		if kuv_pc != .
		replace high_iq				= 1 		if kuv_pc >= 90 & kuv_pc != .

		gen treated_iq				= treated * high_iq
		gen treated_post_iq			= treated_post * high_iq
		}
		
	gen star_inv				= 0
	replace star_inv			= 1 			if fwd_cits5_max >= 5 &  fwd_cits5_max != .

	gen birth_year				= vuosi - ika
	gen year					= vuosi
	sort shnro vuosi
	bysort shnro: gen within_ind		= _n
	tabstat birth_year if within_ind == 1, stat(mean p50)
	gen interact				= 0

	replace interact			= vuosi - suorv
	
	gen msc_new					= 0 
	replace msc_new 			= 1 			if  edu_lev >= 4 & edu_lev != .
	
	gen treated_ia				= treated * interact
	gen treated_post_ia			= treated_post * interact

	gen treated_pre				= treated * (f_1 + f_2 + f_3 + f_4)	
	gen treated_pre_ia			= treated_pre * interact
	if   $data_int == 6 {
		gen treated_pre_iq		= treated_pre * high_iq
		}
	* name interaction
	local inter					= "suorv"
	
	* NOTE: using syrtun or sykstun makes no difference

	sort shnro vuosi
	gen djob_d					= .
	replace djob_d				= 0 			if shnro == shnro[_n - 1] & syrtun == syrtun[_n - 1] & syrtun != "" & syrtun[_n - 1] != ""
	replace djob_d				= 1 			if shnro == shnro[_n - 1] & syrtun != syrtun[_n - 1] & syrtun != "" & syrtun[_n - 1] != ""
	
	if $ownfirm == 1 {
		sort shnro vuosi
		replace ue_d 			= ue_d[_n + 1] 		if ue[_n + 1] == 1 		& shnro == shnro[_n + 1] & syrtun != syrtun[_n - 1] & syrtun != "" & syrtun[_n - 1] != ""
		replace djob_d 			= djob_d[_n + 1] 	if djob_d[_n + 1] == 1 	& shnro == shnro[_n + 1] & syrtun != syrtun[_n - 1] & syrtun != "" & syrtun[_n - 1] != "" 
		}
	global dep_vars lnwage

	if `datacounter' == 1 {
	
		preserve
		use "${data_path}\returns_DATAPREP_1.dta", clear
		drop 									if syrtun 	== ""
		keep syrtun vuosi inv_1
		bysort syrtun: egen total_inv_1		= total(inv_1)
		
		keep syrtun vuosi total_inv_1
		
		bysort syrtun vuosi: gen index 		= _n
		keep 									if index == 1
		drop index
		save "${data_path}\returns_inventor_count_data.dta", replace
		restore
		}
	else if `datacounter' != 1 {
		drop _merge
		merge m:1 syrtun using "${data_path}\returns_inventor_count_data.dta"
		replace total_inv_1		= 3000 			if syrtun == ""
		drop _merge
		}

	gen est_sample 				= .
	replace est_sample			= .
	replace est_sample 			= 1  			if cem_year != . & treatment_time >= -5 & treatment_time <= 10 & syrtun != "" & total_firm_size != . ///
												& msc != . & edu_science !=. & manuf !=. & region_large !=. & age !=. // & iq !=.
	gen est_sample_rob 			= .
	replace est_sample_rob		= 1  			if cem_year != . & treatment_time >= -5 & treatment_time <= 10 & syrtun != "" & firm_size != . ///
												& msc != . & edu_science !=. & manuf !=. & region_large !=. & age !=. // & iq !=.
	gen est_sample_top3 		= .
	replace est_sample_top3 	= 1  			if cem_year != . & treatment_time >= -5 & treatment_time <= 10 & syrtun != "" & total_firm_size != . ///
												& msc != . & edu_science !=. & manuf !=. & region_large !=. & age !=. & total_inv_1 < 850 // & iq !=.
	gen est_sample_long			= .
	replace est_sample_long		= 1  			if cem_year != . & treatment_time >= -8 & treatment_time <= 10 & syrtun != "" & total_firm_size != . ///
												& msc != . & edu_science !=. & manuf !=. & region_large !=. & age !=. // & iq !=.
	gen est_sample_publ			= .
	replace est_sample_publ 	= 1  			if cem_year != . & treatment_time >= -5 & treatment_time <= 10 & total_firm_size != . ///
												& msc != . & edu_science !=. & manuf !=. & region_large !=. & age !=.  // & iq !=.
	
	
	* RESTAT REVISION	
	* what fraction of data are PhD?
	tab educ_level 		if est_sample == 1 /* educ_level = 8 == PhD */
	tab edu_science 	if est_sample == 1
	
	
	if $ownfirm == 1 {
		egen syrtun_num			= group(syrtun)
		gen syrtun_l0_0			= syrtun_num 	if l_0 == 1
		
		bysort shnro: egen syrtun_l0 = max(syrtun_l0_0)
		
		replace est_sample 		= . 			if syrtun_num != syrtun_l0
		replace est_sample_rob	= . 			if syrtun_num != syrtun_l0
		replace est_sample_top3	= . 			if syrtun_num != syrtun_l0
		replace est_sample_long	= . 			if syrtun_num != syrtun_l0
		replace est_sample_publ	= . 			if syrtun_num != syrtun_l0
		}
		
	global dep_vars lnwage
	global firm_size_vars firm_size_new // firm_size_d		
	
	capture: drop interact birth_year
	
	gen birth_year				= vuosi - ika
	sort shnro vuosi
	tabstat birth_year 							if within_ind == 1, stat(mean p50)

	global short_depvar ue_d 
	
	tabstat $short_depvar if est_sample == 1, stat(mean sd p50 n)
	tabstat $short_depvar if est_sample == 1, stat(mean) by(vuosi)

	gen categ					= .
	replace categ				= 0 			if after == 0
	replace categ				= 1 			if treated == 0 & after == 0
	replace categ				= 2 			if treated == 0 & after == 1
	replace categ				= 3 			if treated == 1 & after == 0
	replace categ				= 4 			if treated == 1 & after == 1
	replace categ				= 5 			if after == 1	
	
	gen bsc						= 0
	replace bsc					= 1 			if edu_lev >= 3 & edu_lev != .
	
	gen est_sample_u	= .
	* generate dummy for working in one of top-3 firms in year of invention
	rename total_inv_1 total_inv_a
	gen total_inv_b 	= total_inv_a * l_0 
	bysort shnro: egen total_inv_1	= max(total_inv_b)

	sort shnro vuosi
	capture: bysort shnro: gen within_shnro_ind = _n		
	

	gen senior			= 0
	replace senior		= 1 					if ika > 30 & ika != .
	if  $data_int == 5 {
		replace senior		= 0
		replace senior		= 1 				if ika > 40 & ika != .
		}	
	gen age_3135		= 0
	replace age_3135	= 1						if ika > 30 & ika <= 35
	gen age_3640		= 0
	replace age_3640	= 1						if ika >= 36 & ika <= 40
	gen age_4145		= 0
	replace age_4145	= 1						if ika > 40 & ika <= 45
	gen age_41			= 0
	replace age_41		= 1						if ika > 40 & ika != .
	gen age_4650		= 0
	replace age_4650	= 1						if ika >= 46 & ika <= 50
	gen age_5155		= 0
	replace age_5155	= 1						if ika > 50 & ika <= 55
	gen age_5660		= 0
	replace age_5660	= 1						if ika >= 56 & ika <= 60
	gen age_61			= 0
	replace age_61		= 1						if ika > 60 & ika != .	
		
	gen age_new			= 2
	replace age_new		= 3 					if ika > 30 & ika <= 40
	replace age_new		= 4 					if ika > 40 & ika != .
	
	
	* 2nd interaction: level of education	
	gen interact2			= 0
	replace interact2		= 1 				if  edu_lev >= 4 & edu_lev != .
	if "`sose_type'" == "bluec" {
		replace interact2	= 1 				if  edu_lev >= 3 & edu_lev != .
		}

	* 3rd interaction: distance to frontier of education
	gen interact3			= vuosi - suorv
	replace interact3		= 0 				if suorv == .
	replace interact3		= ika - 15 			if interact3 > ika - 15 & interact3 != . & suorv != .
	
	gen dtf_610				= 0
	replace dtf_610			= 1 				if interact3 > 5 & interact3 <= 10
	gen dtf_1115			= 0
	replace dtf_1115		= 1 				if interact3 > 10 & interact3 <= 15
	gen dtf_1620			= 0
	replace dtf_1620		= 1 				if interact3 > 15 & interact3 <= 20
	gen dtf_2125			= 0
	replace dtf_2125		= 1 				if interact3 > 20 & interact3 <= 25
	gen dtf_2630			= 0
	replace dtf_2630		= 1 				if interact3 > 25 & interact3 <= 30
	gen dtf_31				= 0
	replace dtf_31			= 1 				if interact3 > 30 & interact3 != .
	
	gen dtf_new				= 5
	replace dtf_new			= 10 				if interact3 > 5 & interact3 <= 10
	replace dtf_new			= 15 				if interact3 > 10 & interact3 <= 15

	replace dtf_new			= 16 				if interact3 > 15 & interact3 != .
	
	gen dtf_cat				= .
	replace dtf_cat			= 1 				if suorv_d == 0 & interact3 <= 5
	replace dtf_cat			= 2 				if suorv_d == 0 & interact3 > 5 & interact3 <= 10
	replace dtf_cat			= 3 				if suorv_d == 0 & interact3 > 10 & interact3 <= 15
	replace dtf_cat			= 4 				if suorv_d == 0 & interact3 > 15 & interact3 <= 20
	replace dtf_cat			= 5 				if suorv_d == 0 & interact3 > 20 & interact3 <= 25
	replace dtf_cat			= 6 				if suorv_d == 0 & interact3 > 25 & interact3 <= 30
	replace dtf_cat			= 7 				if suorv_d == 0 & interact3 > 30
	

	* 4th and 5th interaction: IQ
	gen post					= after
	gen pre						= f_1 + f_2 + f_3 + f_4
	
	if  $data_int == 6 {
		gen interact4				= 0
		replace interact4			= 1  if p_test_pc > 50 & p_test_pc <= 90
		gen interact5				= 0
		replace interact5			= 1  if p_test_pc > 90 & p_test_pc != .
		}
		
	gen pre_iasenior			= pre * senior
	gen pre_ia1_41				= pre * age_41
	gen pre_ia1_4145			= pre * age_4145
	gen pre_ia1_4650			= pre * age_4650
	gen pre_ia1_5155			= pre * age_5155
	gen pre_ia1_5660			= pre * age_5660
	gen pre_ia1_61				= pre * age_61
		
	gen pre_ia2					= pre * interact2
	gen pre_ia3					= pre * interact3
	gen pre_ia3_610				= pre * dtf_610
	gen pre_ia3_1115			= pre * dtf_1115
	gen pre_ia3_1620			= pre * dtf_1620
	gen pre_ia3_2125			= pre * dtf_2125
	gen pre_ia3_2630			= pre * dtf_2630
	gen pre_ia3_31				= pre * dtf_31
	gen pre_iasuorv_d			= pre * suorv_d
	
	if  $data_int == 6 {
		gen pre_ia4					= pre * interact4
		gen pre_ia5					= pre * interact5
		}
		
	gen post_iasenior			= post * senior
	gen post_ia1_41				= post * age_41
	gen post_ia1_4145			= post * age_4145
	gen post_ia1_4650			= post * age_4650
	gen post_ia1_5155			= post * age_5155
	gen post_ia1_5660			= post * age_5660
	gen post_ia1_61				= post * age_61
		
	gen post_ia2				= post * interact2
	gen post_ia3				= post * interact3
	gen post_ia3_610			= post * dtf_610
	gen post_ia3_1115			= post * dtf_1115
	gen post_ia3_1620			= post * dtf_1620
	gen post_ia3_2125			= post * dtf_2125
	gen post_ia3_2630			= post * dtf_2630
	gen post_ia3_31				= post * dtf_31
	gen post_iasuorv_d			= post * suorv_d
	
	if  $data_int == 6 {
		gen post_ia4				= post * interact4
		gen post_ia5				= post * interact5
		}
		
	gen treated_iasenior		= treated * senior
	
	gen treated_ia1_41			= treated * age_41
	gen treated_ia1_4145		= treated * age_4145
	gen treated_ia1_4650		= treated * age_4650
	gen treated_ia1_5155		= treated * age_5155
	gen treated_ia1_5660		= treated * age_5660
	gen treated_ia1_61			= treated * age_61
	gen treated_ia1_5160		= treated_ia1_5155 + treated_ia1_5660
	
	gen treated_ia2				= treated * interact2
	
	gen treated_ia3				= treated * interact3
	gen treated_ia3_610			= treated * dtf_610 
	gen treated_ia3_1115		= treated * dtf_1115
	gen treated_ia3_1620		= treated * dtf_1620
	gen treated_ia3_2125		= treated * dtf_2125
	gen treated_ia3_2630		= treated * dtf_2630
	gen treated_ia3_31			= treated * dtf_31
	
	gen treated_iasuorv_d		= treated * suorv_d
	
	if  $data_int == 6 {
		gen treated_ia4				= treated * interact4
		gen treated_ia5				= treated * interact5
		}
		
	gen treated_post_iasenior	= treated_post * senior
	gen treated_post_ia1_41		= treated_post * age_41
	gen treated_post_ia1_4145	= treated_post * age_4145
	gen treated_post_ia1_4650	= treated_post * age_4650
	gen treated_post_ia1_5155	= treated_post * age_5155
	gen treated_post_ia1_5660	= treated_post * age_5660
	gen treated_post_ia1_61		= treated_post * age_61
	
	gen treated_post_ia2		= treated_post * interact2
	
	gen treated_post_ia3		= treated_post * interact3
	gen treated_post_ia3_610	= treated_post * dtf_610
	gen treated_post_ia3_1115	= treated_post * dtf_1115
	gen treated_post_ia3_1620	= treated_post * dtf_1620
	gen treated_post_ia3_2125	= treated_post * dtf_2125
	gen treated_post_ia3_2630	= treated_post * dtf_2630
	gen treated_post_ia3_31		= treated_post * dtf_31
	
	gen treated_post_iasuorv_d	= treated_post * suorv_d
	
	if  $data_int == 6 {
		gen treated_post_ia4		= treated_post * interact4
		gen treated_post_ia5		= treated_post * interact5
		}
		
	gen treated_pre_iasenior	= treated_pre * senior
	gen treated_pre_ia1_41		= treated_pre * age_41
	gen treated_pre_ia1_4145	= treated_pre * age_4145
	gen treated_pre_ia1_4650	= treated_pre * age_4650
	gen treated_pre_ia1_5155	= treated_pre * age_5155
	gen treated_pre_ia1_5660	= treated_pre * age_5660
	gen treated_pre_ia1_61		= treated_pre * age_61
	
	gen treated_pre_ia2			= treated_pre * interact2
	
	gen treated_pre_ia3			= treated_pre * interact3
	gen treated_pre_ia3_610		= treated_pre * dtf_610
	gen treated_pre_ia3_1115	= treated_pre * dtf_1115
	gen treated_pre_ia3_1620	= treated_pre * dtf_1620
	gen treated_pre_ia3_2125	= treated_pre * dtf_2125
	gen treated_pre_ia3_2630	= treated_pre * dtf_2630
	gen treated_pre_ia3_31		= treated_pre * dtf_31
	
	gen treated_pre_iasuorv_d	= treated_pre * suorv_d
	
	gen post_ia2_senior			= post * interact2 * senior
	gen pre_ia2_senior			= pre * interact2 * senior
	gen post_ia2_ia3			= post * interact2 * interact3
	gen pre_ia2_ia3				= pre * interact2 * interact3
	
	gen treated_post_ia2_senior	= treated_post * interact2 * senior
	gen treated_pre_ia2_senior	= treated_pre * interact2 * senior
	gen treated_post_ia2_ia3	= treated_post * interact2 * interact3
	gen treated_pre_ia2_ia3		= treated_pre * interact2 * interact3
	gen ia2_ia3					= interact2 * interact3
	gen ia2_senior				= interact2 * senior
	
	gen treated_ia2_senior		= treated * interact2 * senior
	gen treated_ia2_ia3			= treated * interact2 * interact3
	
	gen ia2_dtf610		= interact2 * dtf_610
	gen ia2_dtf1115		= interact2 * dtf_1115
	gen ia2_dtf1620		= interact2 * dtf_1620
	gen ia2_dtf2125		= interact2 * dtf_2125
	gen ia2_dtf2630		= interact2 * dtf_2630
	gen ia2_dtf31		= interact2 * dtf_31
	
	gen treated_ia2_dtf610		= treated * interact2 * dtf_610
	gen treated_ia2_dtf1115		= treated * interact2 * dtf_1115
	gen treated_ia2_dtf1620		= treated * interact2 * dtf_1620
	gen treated_ia2_dtf2125		= treated * interact2 * dtf_2125
	gen treated_ia2_dtf2630		= treated * interact2 * dtf_2630
	gen treated_ia2_dtf31		= treated * interact2 * dtf_31
	
	gen pre_ia2_dtf610	= pre * interact2 * dtf_610
	gen pre_ia2_dtf1115	= pre * interact2 * dtf_1115
	gen pre_ia2_dtf1620	= pre * interact2 * dtf_1620
	gen pre_ia2_dtf2125	= pre * interact2 * dtf_2125
	gen pre_ia2_dtf2630	= pre * interact2 * dtf_2630
	gen pre_ia2_dtf31	= pre * interact2 * dtf_31
	
	gen post_ia2_dtf610	= post * interact2 * dtf_610
	gen post_ia2_dtf1115	= post * interact2 * dtf_1115
	gen post_ia2_dtf1620	= post * interact2 * dtf_1620
	gen post_ia2_dtf2125	= post * interact2 * dtf_2125
	gen post_ia2_dtf2630	= post * interact2 * dtf_2630
	gen post_ia2_dtf31		= post * interact2 * dtf_31

	gen treated_pre_ia2_dtf610	= treated_pre * interact2 * dtf_610
	gen treated_pre_ia2_dtf1115	= treated_pre * interact2 * dtf_1115
	gen treated_pre_ia2_dtf1620	= treated_pre * interact2 * dtf_1620
	gen treated_pre_ia2_dtf2125	= treated_pre * interact2 * dtf_2125
	gen treated_pre_ia2_dtf2630	= treated_pre * interact2 * dtf_2630
	gen treated_pre_ia2_dtf31	= treated_pre * interact2 * dtf_31
	
	gen treated_post_ia2_dtf610		= treated_post * interact2 * dtf_610
	gen treated_post_ia2_dtf1115	= treated_post * interact2 * dtf_1115
	gen treated_post_ia2_dtf1620	= treated_post * interact2 * dtf_1620
	gen treated_post_ia2_dtf2125	= treated_post * interact2 * dtf_2125
	gen treated_post_ia2_dtf2630	= treated_post * interact2 * dtf_2630
	gen treated_post_ia2_dtf31		= treated_post * interact2 * dtf_31

	gen ia2_age4145		= interact2 * age_4145
	gen ia2_age4650		= interact2 * age_4650
	gen ia2_age5155		= interact2 * age_5155
	gen ia2_age5660		= interact2 * age_5660
	
	gen treated_ia2_age4145		= treated * interact2 * age_4145
	gen treated_ia2_age4650		= treated * interact2 * age_4650
	gen treated_ia2_age5155		= treated * interact2 * age_5155
	gen treated_ia2_age5660		= treated * interact2 * age_5660
	
	gen pre_ia2_age4145		= pre * interact2 * age_4145
	gen pre_ia2_age4650		= pre * interact2 * age_4650
	gen pre_ia2_age5155		= pre * interact2 * age_5155
	gen pre_ia2_age5660		= pre * interact2 * age_5660
	
	gen post_ia2_age4145	= post * interact2 * age_4145
	gen post_ia2_age4650	= post * interact2 * age_4650
	gen post_ia2_age5155	= post * interact2 * age_5155
	gen post_ia2_age5660	= post * interact2 * age_5660
	
	gen treated_pre_ia2_age4145		= treated_pre * interact2 * age_4145
	gen treated_pre_ia2_age4650		= treated_pre * interact2 * age_4650
	gen treated_pre_ia2_age5155		= treated_pre * interact2 * age_5155
	gen treated_pre_ia2_age5660		= treated_pre * interact2 * age_5660
	
	gen treated_post_ia2_age4145	= treated_post * interact2 * age_4145
	gen treated_post_ia2_age4650	= treated_post * interact2 * age_4650
	gen treated_post_ia2_age5155	= treated_post * interact2 * age_5155
	gen treated_post_ia2_age5660	= treated_post * interact2 * age_5660
	
	if  $data_int == 6 {
		gen treated_pre_ia4			= treated_pre * interact4
		gen treated_pre_ia5			= treated_pre * interact5
		}
		
	global interactions 		interact2 dtf_610 dtf_1115 dtf_16
	
	global pre_ias_short		pre_iasenior pre_ia2 pre_ia3 pre_iasuorv_d
	global pre_ias_educ			pre_ia2 pre_ia3 pre_iasuorv_d
	
	global pre_ias_med			pre_ia1_41 pre_ia2 pre_ia3 pre_iasuorv_d // pre_ia1_3135 pre_ia1_3640
	global pre_ias_meduc	 	pre_ia2 pre_ia3_610 pre_ia3_1115 pre_ia3_1620 pre_ia3_2125 pre_ia3_2630 pre_ia3_31 pre_iasuorv_d
	global pre_ias_long			pre_ia1_41 pre_ia2 pre_ia3_610 pre_ia3_1115 pre_ia3_16 pre_iasuorv_d // pre_ia1_3135 pre_ia1_3640
	
	if $data_int == 5 {
		global pre_ias_med			pre_ia1_4145 pre_ia1_4650 pre_ia1_5155 pre_ia1_5660 pre_ia1_61 pre_ia2 pre_ia3 pre_iasuorv_d // pre_ia1_3135 pre_ia1_3640
		global pre_ias_long			pre_ia1_4145 pre_ia1_4650 pre_ia1_5155 pre_ia1_5660 pre_ia1_61 pre_ia2 ///		// pre_ia1_3135 pre_ia1_3640   
									pre_ia3_610 pre_ia3_1115 pre_ia3_1620 pre_ia3_2125 pre_ia3_2630 pre_ia3_31 pre_iasuorv_d
		}
		
	global post_ias_short		post_iasenior post_ia2 post_ia3 post_iasuorv_d
	global post_ias_educ		post_ia2 post_ia3 post_iasuorv_d
	global post_ias_med			post_ia1_41 post_ia2 post_ia3 post_iasuorv_d // post_ia1_3135 post_ia1_3640
	global post_ias_meduc		post_ia2 post_ia3_610 post_ia3_1115 post_ia3_1620 post_ia3_2125 post_ia3_2630 post_ia3_31 post_iasuorv_d
	global post_ias_long		post_ia1_41 post_ia2  /// 	// post_ia1_3135 post_ia1_3640
								post_ia3_610 post_ia3_1115 post_ia3_1620 post_ia3_2125 post_ia3_2630 post_ia3_31 post post_iasuorv_d
	
	if $data_int == 5 {
		global post_ias_med			post_ia1_4145 post_ia1_4650 post_ia1_5155 post_ia1_5660 post_ia1_61 post_ia2 post_ia3 post_iasuorv_d 	// post_ia1_3135 post_ia1_3640 
		global post_ias_long		post_ia1_4145 post_ia1_4650 post_ia1_5155 post_ia1_5660 post_ia1_61 post_ia2  /// 	// post_ia1_3135 post_ia1_3640
									post_ia3_610 post_ia3_1115 post_ia3_1620 post_ia3_2125 post_ia3_2630 post_ia3_31 post post_iasuorv_d
		}
		
	global treated_educ 			treated_ia2 treated_ia3_* treated_suorv_d
	global treated_post_educ_ia		treated_post_ia2 treated_post_ia3_* post_iasuorv_d
	global treated_pre_educ_ia 		treated_pre_ia2 treated_pre_ia3_* post_iasuorv_d
	
	global treated_age			treated_ia1_41 // treated_ia1_3135 treated_ia1_3640 
	global treated_pre_age_ia	treated_pre_ia1_41 // treated_pre_ia1_3135 treated_pre_ia1_3640 
	global treated_post_age_ia	treated_post_ia1_41 // treated_post_ia1_3135 treated_post_ia1_3640 
	
	if $data_int == 5 {
		global treated_age		 	treated_ia1_4145 treated_ia1_4650 treated_ia1_5160 // treated_ia1_3135 treated_ia1_3640 
		global treated_pre_age_ia	treated_pre_ia1_4145 treated_pre_ia1_4650 treated_pre_ia1_5155 treated_pre_ia1_5660 treated_pre_ia1_61 // treated_pre_ia1_3135 treated_pre_ia1_3640 
		global treated_post_age_ia	treated_post_ia1_4145 treated_post_ia1_4650 treated_post_ia1_5155 treated_post_ia1_5660 treated_post_ia1_61 // treated_post_ia1_3135 treated_post_ia1_3640 
		}
		
	if  $data_int == 6 {
		global interactions_iq 		interact2 dtf_610 dtf_1115 dtf_16 interact4 interact5
		global treated_ias_iq 		treated_ia1_* treated_ia2 treated_ia3_* treated_ia4 treated_ia5
		global treated_post_ias_iq	treated_post_ia1_* treated_post_ia2 treated_post_ia3_* treated_post_ia4 treated_post_ia5
		global treated_pre_ias_iq 	treated_pre_ia1_* treated_pre_ia2 treated_pre_ia3_* treated_pre_ia4 treated_pre_ia5
		}
	
	global ia2_dtfias				ia2_dtf*
	global pre_ia2_dtfias			pre_ia2_dtf*
	global post_ia2_dtfias			post_ia2_dtf*
	
	global treated_ia2_dtfias		treated_ia2_dtf*
	global treated_pre_ia2_dtfias	treated_pre_ia2_dtf*
	global treated_post_ia2_dtfias	treated_post_ia2_dtf*
	
	global ia2_ageias				ia2_age*
	global pre_ia2_ageias			pre_ia2_age*
	global post_ia2_ageias			post_ia2_age*	
	
	global treated_ia2_ageias		treated_ia2_age*
	global treated_pre_ia2_ageias	treated_pre_ia2_age*
	global treated_post_ia2_ageias	treated_post_ia2_age*
	
	*global interactions_long l_ia* inv_f_ia_* f_ia_*
	global leads_and_lags l_0 l_1 l_2 l_3 l_4 l_5 l_6 l_7 l_8 l_9 l_10 f_1 f_2 f_3 f_4
	
	foreach t of global interactions {
		display "number of obs by treatment category conditional on `t'"
		tab categ `t' if est_sample_u == 1 
		}
		
	foreach d of global short_depvar {
		
			replace est_sample_u 		= est_sample
			* need to use est_sample_publ for ue_d as otherwise ue_d == 0 by definition
			if "`d'" == "ue_d" {
				replace est_sample_u	= est_sample_publ
				}
				
			tab ptoim1 														if  est_sample_u == 1	
		
			regr `d' 	treated $treated_age ///
				treated_pre ///
				$treated_pre_age_ia   ///
				treated_post ///
				$treated_post_age_ia  ///
				$leads_and_lags $calendar_year_ds $age_ds $firm_size_vars  	if est_sample_u == 1, cluster(shnro) 
			
			replace est_sample_u = e(sample)
		
			display "dependent variable is `d'"	

			* Appendix A.2 descriptive statistics
			
			tabstat $short_depvar ika bsc msc dtf  				if est_sample_u == 1 & treated == 0, stat(mean sd p50 n) save
			matrix A = r(StatTotal)
			tabstat $short_depvar ika bsc msc dtf  				if est_sample_u == 1 & treated == 1, stat(mean sd p50 n) save
			matrix B = r(StatTotal)
			tabstat $short_depvar ika bsc msc dtf  				if est_sample_u == 1, stat(mean sd p50 n) by(treated) save
			matrix C = r(StatTotal)
			
			putexcel set "${graphs}\Appendix_A.2_tabstat_`sose_type'$data_`d'_noIQ.xls", replace
			putexcel B1 = "treated = 0"
			putexcel B2 = matrix(A), names
			putexcel B8 = "treated = 1"
			putexcel B9 = matrix(B), names
			putexcel B15 = "total"
			putexcel B16 = matrix(C), names
			
		
			
			*display " 0 = before, 1 = control before, 2 = control after, 3 = treated before, 4 = treated after 5 = after "

			* Appendix A.3 desc stats on distribution of age and dtf
			tab age_new dtf_cat if est_sample_u == 1 , cell

			tab dtf 	if ika == 35 & est_sample_u == 1 
			/*
			twoway 	histogram dtf if ika == 35 & est_sample_u == 1, ///
						xtitle("distance to human capital frontier")  ///
						graphregion(color(white)) bgcolor(white)
					graph export "${graphs}\dtf_35_`sose_type'$data_`d'.pdf" , replace
			*/
			tab dtf 	if ika == 45 & est_sample_u == 1 
			/*
			twoway 	histogram dtf if ika == 45 & est_sample_u == 1, ///
						xtitle("distance to human capital frontier")  ///
						graphregion(color(white)) bgcolor(white)
					graph export "${graphs}\dtf_45_`sose_type'$data_`d'.pdf" , replace
			*/		
			tab dtf 	if ika == 55 & est_sample_u == 1 
			/*
			twoway 	histogram dtf if ika == 55 & est_sample_u == 1, ///
						xtitle("distance to human capital frontier")  ///
						graphregion(color(white)) bgcolor(white)
					graph export "${graphs}\dtf_55_`sose_type'$data_`d'.pdf" , replace
			*/
			bysort ika: egen dtf_p10		= pctile(dtf)				if est_sample_u == 1, p(10)
			*bysort ika: egen dtf_p25		= pctile(dtf)				if est_sample_u == 1, p(25)
			*bysort ika: egen dtf_p50		= pctile(dtf)				if est_sample_u == 1, p(50)
			*bysort ika: egen dtf_p75		= pctile(dtf)				if est_sample_u == 1, p(75)
			bysort ika: egen dtf_p90		= pctile(dtf)				if est_sample_u == 1, p(90)
			*bysort ika: egen dtf_mean		= mean(dtf)					if est_sample_u == 1
			bysort ika: gen ika_ind			= _n						if est_sample_u == 1
			
			twoway rarea dtf_p10 dtf_p90 ika	if est_sample_u == 1 & ika_ind == 1	, ///
					sort fcolor(gs10) ///
					xtitle("age")  ///
					ytitle("DTHCF") ///
					lwidth(none) /// 					
					graphregion(color(white)) bgcolor(white) ///
					legend( order(2 3) label(2 "10th percentile") label(3 "90th percentile")) ///
					||   line dtf_p10 ika if est_sample_u == 1 & ika_ind == 1 ///
					||   line dtf_p90 ika if est_sample_u == 1 & ika_ind == 1 
							graph export "${graphs}\dtf_age_`sose_type'$data_`d'_noIQ.pdf" , replace	

					
		
			*drop dtf_p10 dtf_p25 dtf_p50 dtf_p75 dtf_mean ika_ind
			drop dtf_p10 dtf_p90 ika_ind

		
			pwcorr ika dtf		 		if est_sample_u == 1 , sig
			pwcorr ika interact3 		if est_sample_u == 1 , sig
			regr dtf ika 				if est_sample_u == 1, robust
			
			pwcorr age_new dtf_cat 		if est_sample_u == 1, sig
			*regr dtf_cat age_new		if est_sample_u == 1, robust
			
			*RESTAT REVISION: ESTIMATE MODEL FOR THOSE WITH STEM EDUCATION
			gen treat_year				= .
			replace treat_year			= year 			if treatment_time == 0
			egen treat_year_count		= group(syrtun treat_year)
			bysort shnro: egen my_group 		= min(treat_year_count)
			drop treat_year treat_year_count

			drop if edu_science == 0	
			if $dummy == 1 {
				
******************************************************************* 			
				* 1. base -  pre + no interaction
******************************************************************* eqn_2
				xtreg `d' 	treated treated_pre treated_post $leads_and_lags $calendar_year_ds $age_ds $firm_size_vars  ///
				if est_sample_u == 1, fe cluster(my_group)  
				estimates store eqn_2		
				
******************************************************************* 
				* 2. base -  pre + simple interaction 1
******************************************************************* 			
				xtreg `d' 	treated treated_iasenior treated_pre treated_pre_iasenior treated_post treated_post_iasenior ///
							senior pre_iasenior post_iasenior $leads_and_lags $calendar_year_ds $age_ds $firm_size_vars   ///
							if est_sample_u == 1, fe cluster(my_group)  
				test treated_post + treated_post_iasenior = 0
				test treated_pre + treated_pre_iasenior = 0
				estimates store eqn_4		

******************************************************************* 	
				* 2. base -  pre + only DTHCF
****************************************************************** eqn_8	
				xtreg `d' 	treated treated_ia3 treated_iasuorv_d ///
							treated_pre  treated_pre_ia3  treated_pre_iasuorv_d ///
							treated_post  treated_post_ia3 treated_post_iasuorv_d ///
							$pre_ia_educ $post_ia_educ ///
							interact3 suorv_d ///
							$leads_and_lags $calendar_year_ds $age_ds $firm_size_vars   if est_sample_u == 1, fe cluster(my_group)  
				test treated_post + treated_post_ia3 = 0
				test treated_pre + treated_pre_ia3 = 0
				estimates store eqn_5		
				
				
				
******************************************************************* 	
				* 2. base -  pre + simple educ
****************************************************************** 

				* senior + DTHCF
				xtreg `d' 	treated treated_iasenior  treated_ia3 treated_iasuorv_d ///
				treated_pre treated_pre_iasenior treated_pre_ia3  treated_pre_iasuorv_d ///
				treated_post treated_post_iasenior treated_post_ia3 treated_post_iasuorv_d ///
				$pre_ia_short $post_ia_short ///
				senior interact3 suorv_d ///
				$leads_and_lags $calendar_year_ds $age_ds $firm_size_vars   if est_sample_u == 1, fe cluster(my_group)  
				estimates store eqn_6		
				testparm *ia3*
				testparm *pre*ia3  *post*ia3 
				testparm *pre*ia3  
				testparm *post*ia3
				test treated_post + treated_post_iasenior = 0
				test treated_post + treated_post_ia3 = 0
				test treated_post + treated_post_iasenior + treated_post_ia3 = 0
				
				* senior + educ + dtf
				xtreg `d' 	treated treated_iasenior treated_ia2 treated_ia3 treated_iasuorv_d ///
				treated_pre treated_pre_iasenior treated_pre_ia2 treated_pre_ia3  treated_pre_iasuorv_d ///
				treated_post treated_post_iasenior treated_post_ia2 treated_post_ia3 treated_post_iasuorv_d ///
				$pre_ia_short $post_ia_short ///
				senior interact2 interact3 suorv_d ///
				$leads_and_lags $calendar_year_ds $age_ds $firm_size_vars   if est_sample_u == 1, fe cluster(my_group)  
				estimates store eqn_7		
				testparm *ia2* *ia3*
				testparm *pre*ia2* *pre*ia3 *post*ia2* *post*ia3 
				testparm *pre*ia2* *pre*ia3  
				testparm *post*ia2* *post*ia3
				test treated_post + treated_post_iasenior = 0
				test treated_post + treated_post_ia2 = 0
				test treated_post + treated_post_ia3 = 0
				test treated_post + treated_post_iasenior + treated_post_ia2 = 0
				test treated_post + treated_post_iasenior + treated_post_ia3 = 0
				test treated_post + treated_post_iasenior + treated_post_ia2 + treated_post_ia3 = 0
				
				sum interact2				if est_sample_u == 1 & senior == 0
				scalar educ_junior_m		= r(mean)
				local educ_junior_m			= educ_junior_m
				local educ_junior_m: di %6.2f	 educ_junior_m
	
				sum interact2				if est_sample_u == 1 & senior == 1
				scalar educ_senior_m		= r(mean)
				local educ_senior_m			= educ_senior_m
				local educ_senior_m: di %6.2f	 educ_senior_m
	
				sum dtf 					if est_sample_u == 1 & senior == 0
				scalar dtf_junior_m			= r(mean)
				local dtf_junior_m			= dtf_junior_m
				local dtf_junior_m: di %6.2f	 dtf_junior_m
	
				sum dtf 					if est_sample_u == 1 & senior == 1
				scalar dtf_senior_m			= r(mean)
				local dtf_senior_m			= dtf_senior_m	
				local dtf_senior_m: di %6.2f	 dtf_senior_m
	
				sum dtf 					if est_sample_u == 1 & senior == 0 & interact2 == 0
				scalar dtf_junior_ml		= r(mean)
				local dtf_junior_ml			= dtf_junior_ml
				local dtf_junior_ml: di %6.2f	 dtf_junior_ml
				
				sum dtf 					if est_sample_u == 1 & senior == 1 & interact2 == 0
				scalar dtf_senior_ml		= r(mean)
				local dtf_senior_ml			= dtf_senior_ml	
				local dtf_senior_ml: di %6.2f	 dtf_senior_ml

				sum dtf 					if est_sample_u == 1 & senior == 0 & interact2 == 1
				scalar dtf_junior_mh		= r(mean)
				local dtf_junior_mh			= dtf_junior_mh	
				local dtf_junior_mh: di %6.2f	 dtf_junior_mh

				sum dtf 					if est_sample_u == 1 & senior == 1 & interact2 == 1
				scalar dtf_senior_mh		= r(mean)
				local dtf_senior_mh			= dtf_senior_mh	
				local dtf_senior_mh: di %6.2f	 dtf_senior_mh
	
	
				*****
				* Graph 1A: junior and senior, mean educ, mean dtf
				nlcom 	(_b[treated_post] + _b[treated_post_iasenior] * 0 + _b[treated_post_ia2] * `educ_junior_m' + _b[treated_post_ia3] * `dtf_junior_m') ///
						(_b[treated_post] + _b[treated_post_iasenior] * 1 + _b[treated_post_ia2] * `educ_senior_m' + _b[treated_post_ia3] * `dtf_senior_m') 
				matrix phat1 = r(b)
				matrix pse1 = vecdiag(r(V))
				matrix CI1 = J(2,2,.)
				forvalues i = 1/2 {
					matrix CI1[1,`i'] = phat1[1, `i'] + sqrt(pse1[1, `i'])
					matrix CI1[2,`i'] = phat1[1, `i'] - sqrt(pse1[1, `i'])
					}	
				
				* Graph 1B: junior and senior x low and high educ x mean dtf | senior, high educ
				nlcom 	(_b[treated_post] + _b[treated_post_iasenior] * 0 + _b[treated_post_ia2] * 0 				 + _b[treated_post_ia3] * `dtf_junior_ml') ///
						(_b[treated_post] + _b[treated_post_iasenior] * 1 + _b[treated_post_ia2] * 0				 + _b[treated_post_ia3] * `dtf_senior_ml') ///
						(_b[treated_post] + _b[treated_post_iasenior] * 0 + _b[treated_post_ia2] * 1 				 + _b[treated_post_ia3] * `dtf_junior_mh') ///
						(_b[treated_post] + _b[treated_post_iasenior] * 1 + _b[treated_post_ia2] * 1				 + _b[treated_post_ia3] * `dtf_senior_mh') 
				matrix phat2 = r(b)
				matrix pse2 = vecdiag(r(V))
				matrix CI2 = J(4,4,.)
				forvalues i = 1/4 {
					matrix CI2[1,`i'] = phat2[1, `i'] + sqrt(pse2[1, `i'])
					matrix CI2[2,`i'] = phat2[1, `i'] - sqrt(pse2[1, `i'])
					}	
					
				* Graph 1C: junior and senior with mean educ and mean dtf; junior and senior x low and high educ x mean dtf | senior, high educ; 
				* junior and senior x  high 	and low educ, dtf = 0							
				nlcom 	(_b[treated_post] + _b[treated_post_iasenior] * 0 + _b[treated_post_ia2] * 0 				 + _b[treated_post_ia3] * 0) ///
						(_b[treated_post] + _b[treated_post_iasenior] * 1 + _b[treated_post_ia2] * 0				 + _b[treated_post_ia3] * 0) ///
						(_b[treated_post] + _b[treated_post_iasenior] * 0 + _b[treated_post_ia2] * 1 				 + _b[treated_post_ia3] * 0) ///
						(_b[treated_post] + _b[treated_post_iasenior] * 1 + _b[treated_post_ia2] * 1				 + _b[treated_post_ia3] * 0) ///
						(_b[treated_post] + _b[treated_post_iasenior] * 0 + _b[treated_post_ia2] * 1 				 + _b[treated_post_ia3] * `dtf_junior_mh') ///
						(_b[treated_post] + _b[treated_post_iasenior] * 1 + _b[treated_post_ia2] * 1				 + _b[treated_post_ia3] * `dtf_senior_mh') ///
						(_b[treated_post] + _b[treated_post_iasenior] * 0 + _b[treated_post_ia2] * 0 				 + _b[treated_post_ia3] * `dtf_junior_ml') ///
						(_b[treated_post] + _b[treated_post_iasenior] * 1 + _b[treated_post_ia2] * 0				 + _b[treated_post_ia3] * `dtf_senior_ml') 
				matrix phat3 = r(b)
				matrix pse3 = vecdiag(r(V))
				matrix CI3 = J(8,8,.)
				forvalues i = 1/8 {
					matrix CI3[1,`i'] = phat3[1, `i'] + sqrt(pse3[1, `i'])
					matrix CI3[2,`i'] = phat3[1, `i'] - sqrt(pse3[1, `i'])
					}
					
			
				coefplot matrix(phat1), ci(CI1) vertical recast(bar)	ciopts(recast(rcap)) ///
						graphregion(color(white)) bgcolor(white)  ///
						ylabel(-0.04(0.1)0.14)	///
						coeflabels(_nl_1 = "JE(`educ_junior_m')D(`dtf_junior_m')" _nl_2 = "SE(`educ_senior_m')D(`dtf_senior_m')") ///
						saving(panelA, replace)
				graph export "${graphs}\Fig1_panelA_`d'_`sose_type'_noIQ_stem_$data.pdf" , replace
				
				coefplot matrix(phat1), ci(CI1) vertical	ciopts(recast(rcap)) ///
						graphregion(color(white)) bgcolor(white)  ///
						ylabel(-0.04(0.1)0.14)	///
						yline(0) ///
						coeflabels(_nl_1 = "JE(`educ_junior_m')D(`dtf_junior_m')" _nl_2 = "SE(`educ_senior_m')D(`dtf_senior_m')") ///
						saving(panelAv2, replace)
				graph export "${graphs}\Fig1_panelAv2_`d'_`sose_type'_noIQ_stem_$data.pdf" , replace

				*		notes("LE = low educ; HE = high educ; J = junior; S = senior")
				* REMEMBER: cols(x) 
				coefplot matrix(phat2), ci(CI2) vertical recast(bar)	ciopts(recast(rcap)) ///
						graphregion(color(white)) bgcolor(white)  ///
						ylabel(-0.04(0.1)0.14)	///
						coeflabels(_nl_1 = " JLD(`dtf_junior_ml')" _nl_2 = "SLD(`dtf_senior_ml')"  ///
						_nl_3 = "JHD(`dtf_junior_mh')" _nl_4 = "SHD(`dtf_senior_mh')") ///
						saving(panelB, replace)
				graph export "${graphs}\Fig1_panelB_`d'_`sose_type'_noIQ_stem_$data.pdf" , replace
				
				coefplot matrix(phat2), ci(CI2) vertical	ciopts(recast(rcap)) ///
						graphregion(color(white)) bgcolor(white)  ///
						ylabel(-0.04(0.1)0.14)	///
						yline(0) ///
						coeflabels(_nl_1 = " JLD(`dtf_junior_ml')" _nl_2 = "SLD(`dtf_senior_ml')"  ///
						_nl_3 = "JHD(`dtf_junior_mh')" _nl_4 = "SHD(`dtf_senior_mh')") ///
						saving(panelBv2, replace)
				graph export "${graphs}\Fig1_panelBv2_`d'_`sose_type'_noIQ_stem_$data.pdf" , replace
				
				coefplot matrix(phat3), ci(CI3) vertical recast(bar) ciopts(recast(rcap))  ///
						graphregion(color(white)) bgcolor(white)  ///
						ylabel(-0.04(0.1)0.14)	///
						coeflabels(_nl_1 = "JLD(0)" _nl_2 = "SLD(0)"  ///
						_nl_3 = "JHD(0)" _nl_4 = "SHD(0)" ///
						_nl_5 = "JHD(`dtf_junior_mh')" _nl_6 = "SHD(`dtf_senior_mh')"  ///
						_nl_7 = "JLD(`dtf_junior_ml')" _nl_8 = "SLD(`dtf_senior_ml')") ///
						saving(panelC, replace)
				graph export "${graphs}\Fig1_panelC_`d'_`sose_type'_noIQ_stem_$data.pdf" , replace
				
				coefplot matrix(phat3), ci(CI3) vertical	ciopts(recast(rcap)) ///
						graphregion(color(white)) bgcolor(white)  ///
						ylabel(-0.04(0.1)0.14)	///
						yline(0) ///
						coeflabels(_nl_1 = "JLD(0)" _nl_2 = "SLD(0)"  ///
						_nl_3 = "JHD(0)" _nl_4 = "SHD(0)" ///
						_nl_5 = "JHD(`dtf_junior_mh')" _nl_6 = "SHD(`dtf_senior_mh')"  ///
						_nl_7 = "JLD(`dtf_junior_ml')" _nl_8 = "SLD(`dtf_senior_ml')") ///
						saving(panelCv2, replace)
				graph export "${graphs}\Fig1_panelCv2_`d'_`sose_type'_noIQ_stem_$data.pdf" , replace

				gr combine panelA.gph panelB.gph, saving(panelAB, replace) imargin(0 0 0 0) cols(2)
				gr combine panelAB.gph panelC.gph, saving(Fig1_all, replace) imargin(tiny) cols(1)
				graph export "${graphs}\Fig1_all_`d'_`sose_type'_noIQ_stem_$data.pdf" , replace
	
				gr combine panelAv2.gph panelBv2.gph, saving(panelABv2, replace) imargin(0 0 0 0) cols(2)
				gr combine panelABv2.gph panelCv2.gph, saving(Fig1v2_all, replace) imargin(tiny) cols(1)
				graph export "${graphs}\Fig1v2_all_`d'_`sose_type'_noIQ_stem_$data.pdf" , replace

				* educ and DTHCF, no senior
				xtreg `d' 	treated treated_ia2 treated_ia3 treated_iasuorv_d ///
				treated_pre  treated_pre_ia2 treated_pre_ia3  treated_pre_iasuorv_d ///
				treated_post  treated_post_ia2 treated_post_ia3 treated_post_iasuorv_d ///
				$pre_ia_educ $post_ia_educ ///
				interact2 interact3 suorv_d ///
				$leads_and_lags $calendar_year_ds $age_ds $firm_size_vars   if est_sample_u == 1, fe cluster(my_group)  
				test treated_post + treated_post_ia2 = 0
				test treated_post + treated_post_ia3 = 0
				test treated_post + treated_post_ia2 + treated_post_ia3 = 0
				test treated_pre + treated_pre_ia2 = 0
				test treated_pre + treated_pre_ia3 = 0
				test treated_pre + treated_pre_ia2 + treated_pre_ia3 = 0
				estimates store eqn_8		

				
				xtreg `d' 	treated treated_iasenior treated_ia2 treated_ia3 treated_iasuorv_d ///
				treated_pre treated_pre_iasenior treated_pre_ia2 treated_pre_ia3  treated_pre_iasuorv_d ///
				treated_post treated_post_iasenior treated_post_ia2 treated_post_ia3 treated_post_iasuorv_d ///
				$pre_ia_short $post_ia_short ///
				senior interact2 interact3 suorv_d ///
				$leads_and_lags $calendar_year_ds $age_ds $firm_size_vars   if est_sample_u == 1, fe cluster(my_group)  
				estimates store eqn_12		
				testparm *ia2* *ia3*
				testparm *pre*ia2* *pre*ia3 *post*ia2* *post*ia3 
				testparm *pre*ia2* *pre*ia3  
				testparm *post*ia2* *post*ia3
				test treated_post + treated_post_iasenior = 0
				test treated_post + treated_post_ia2 = 0
				test treated_post + treated_post_ia3 = 0
				test treated_post + treated_post_iasenior + treated_post_ia2 = 0
				test treated_post + treated_post_iasenior + treated_post_ia3 = 0
				test treated_post + treated_post_iasenior + treated_post_ia2 + treated_post_ia3 = 0

		
			

******************************************************************* 
				* 8 base - interaction drop top-3
				
				* 1. base -  pre + no interaction
******************************************************************* 
				xtreg `d' 	treated treated_pre treated_post $leads_and_lags $calendar_year_ds $age_ds $firm_size_vars  ///
				if est_sample_u == 1 & total_inv_1 < 850, fe cluster(my_group)  
				estimates store eqn_top32		
				
******************************************************************* 
				* 2. base -  pre + simple interaction 1
******************************************************************* 				
				xtreg `d' 	treated treated_ia3 treated_iasuorv_d ///
					treated_pre  treated_pre_ia3  treated_pre_iasuorv_d ///
					treated_post  treated_post_ia3 treated_post_iasuorv_d ///
					$pre_ia_educ $post_ia_educ ///
					interact3 suorv_d ///
					$leads_and_lags $calendar_year_ds $age_ds $firm_size_vars   if est_sample_u == 1  & total_inv_1 < 850, fe cluster(my_group)  
				test treated_post + treated_post_ia3 = 0
				test treated_pre + treated_pre_ia3 = 0
				estimates store eqn_top33		
				
				xtreg `d' 	treated treated_iasenior treated_pre treated_pre_iasenior treated_post treated_post_iasenior ///
							senior pre_iasenior post_iasenior $leads_and_lags $calendar_year_ds $age_ds $firm_size_vars   ///
							if est_sample_u == 1 & total_inv_1 < 850, fe cluster(my_group)  
				test treated_post + treated_post_iasenior = 0
				test treated_pre + treated_pre_iasenior = 0
				estimates store eqn_top34	
				
				
******************************************************************* 				
				* 2. base -  pre + simple educ
******************************************************************* 	
				xtreg `d' 	treated treated_ia2 treated_ia3 treated_iasuorv_d ///
							treated_pre  treated_pre_ia2 treated_pre_ia3  treated_pre_iasuorv_d ///
							treated_post  treated_post_ia2 treated_post_ia3 treated_post_iasuorv_d ///
							$pre_ia_educ $post_ia_educ ///
							interact2 interact3 suorv_d ///
							$leads_and_lags $calendar_year_ds $age_ds $firm_size_vars   if est_sample_u == 1 & total_inv_1 < 850, fe cluster(my_group)  
				test treated_post + treated_post_ia2 = 0
				test treated_post + treated_post_ia3 = 0
				test treated_post + treated_post_ia2 + treated_post_ia3 = 0
				test treated_pre + treated_pre_ia2 = 0
				test treated_pre + treated_pre_ia3 = 0
				test treated_pre + treated_pre_ia2 + treated_pre_ia3 = 0
				estimates store eqn_top35						

				
				* senior + DTHCF
				xtreg `d' 	treated treated_iasenior  treated_ia3 treated_iasuorv_d ///
				treated_pre treated_pre_iasenior treated_pre_ia3  treated_pre_iasuorv_d ///
				treated_post treated_post_iasenior treated_post_ia3 treated_post_iasuorv_d ///
				$pre_ia_short $post_ia_short ///
				senior interact3 suorv_d ///
				$leads_and_lags $calendar_year_ds $age_ds $firm_size_vars   if est_sample_u == 1 & total_inv_1 < 850, fe cluster(my_group)  
				estimates store eqn_top36		
				testparm *ia3*
				testparm *pre*ia3  *post*ia3 
				testparm *pre*ia3  
				testparm *post*ia3
				test treated_post + treated_post_iasenior = 0
				test treated_post + treated_post_ia3 = 0
				test treated_post + treated_post_iasenior + treated_post_ia3 = 0

				* senior + educ
				xtreg `d' 	treated treated_iasenior treated_ia2 treated_ia3 treated_iasuorv_d ///
				treated_pre treated_pre_iasenior treated_pre_ia2 treated_pre_ia3  treated_pre_iasuorv_d ///
				treated_post treated_post_iasenior treated_post_ia2 treated_post_ia3 treated_post_iasuorv_d ///
				$pre_ia_short $post_ia_short ///
				senior interact2 interact3 suorv_d ///
				$leads_and_lags $calendar_year_ds $age_ds $firm_size_vars   if est_sample_u == 1 & total_inv_1 < 850, fe cluster(my_group)  
				estimates store eqn_top37		
				testparm *ia2* *ia3*
				testparm *pre*ia2* *pre*ia3 *post*ia2* *post*ia3 
				testparm *pre*ia2* *pre*ia3  
				testparm *post*ia2* *post*ia3
				test treated_post + treated_post_iasenior = 0
				test treated_post + treated_post_ia2 = 0
				test treated_post + treated_post_ia3 = 0
				test treated_post + treated_post_iasenior + treated_post_ia2 = 0
				test treated_post + treated_post_iasenior + treated_post_ia3 = 0
				test treated_post + treated_post_iasenior + treated_post_ia2 + treated_post_ia3 = 0
				
				* educ and DTHCF, no senior
				xtreg `d' 	treated treated_ia2 treated_ia3 treated_iasuorv_d ///
				treated_pre  treated_pre_ia2 treated_pre_ia3  treated_pre_iasuorv_d ///
				treated_post  treated_post_ia2 treated_post_ia3 treated_post_iasuorv_d ///
				$pre_ia_educ $post_ia_educ ///
				interact2 interact3 suorv_d ///
				$leads_and_lags $calendar_year_ds $age_ds $firm_size_vars   if est_sample_u == 1, fe cluster(my_group)  
				test treated_post + treated_post_ia2 = 0
				test treated_post + treated_post_ia3 = 0
				test treated_post + treated_post_ia2 + treated_post_ia3 = 0
				test treated_pre + treated_pre_ia2 = 0
				test treated_pre + treated_pre_ia3 = 0
				test treated_pre + treated_pre_ia2 + treated_pre_ia3 = 0
				estimates store eqn_top38		


****************************************************************************************************************************************************************				

				local filename		= "`d'"
				cd ${output}

				if $ownfirm != 1 & $data_int == 6 {
	 					outreg2 [eqn_2 eqn_4 eqn_5 eqn_6 eqn_7 eqn_8 eqn_12] ///
						using "`d'_`sose_type'_age_short_noIQ_stem_$data.xls", ///
						replace keep(treated_pre treated_pre_ia*se* treated_pre_ia1* treated_pre_ia2* treated_pre_ia* treated_pre_ia*su*  ///
										treated_post treated_post_ia*se* treated_post_ia1* treated_post_ia2* treated_post_ia* treated_post_ia*su* ///
										interact* treated treated_ia*se* treated_ia1* treated_ia2* treated_ia* treated_ia*su*)
						}
				else if $ownfirm != 1 & $data_int == 5 	{
						outreg2 [eqn_2 eqn_4 eqn_5 eqn_6 eqn_7 eqn_8 eqn_12] ///
						using "`d'_`sose_type'_age_short_noIQ_stem_$data.xls", ///
						replace keep(treated_pre treated_pre_ia*se* treated_pre_ia1* treated_pre_ia2* treated_pre_ia* treated_pre_ia*su*  ///
										treated_post treated_post_ia*se* treated_post_ia1* treated_post_ia2* treated_post_ia* treated_post_ia*su* ///
										interact* treated treated_ia*se* treated_ia1* treated_ia2* treated_ia* treated_ia*su*)
						}	
				if $ownfirm != 1 & $data_int == 6 {
	 					outreg2 [eqn_top32 eqn_top33 eqn_top34 eqn_top35 eqn_top36 eqn_top37 eqn_top38] ///
						using "`d'_`sose_type'_age_top3_noIQ_stem_$data.xls", ///
						replace keep(treated_pre treated_pre_ia*se* treated_pre_ia1* treated_pre_ia2* treated_pre_ia* treated_pre_ia*su*  ///
										treated_post treated_post_ia*se* treated_post_ia1* treated_post_ia2* treated_post_ia* treated_post_ia*su* ///
										interact* treated treated_ia*se* treated_ia1* treated_ia2* treated_ia* treated_ia*su*)
						}
				else if $ownfirm != 1 & $data_int == 5 	{
						outreg2 [eqn_top32 eqn_top33 eqn_top34 eqn_top35 eqn_top36 eqn_top37 eqn_top38] ///
						using "`d'_`sose_type'_age_top3_noIQ_stem_$data.xls", ///
						replace keep(treated_pre treated_pre_ia*se* treated_pre_ia1* treated_pre_ia2* treated_pre_ia* treated_pre_ia*su*  ///
										treated_post treated_post_ia*se* treated_post_ia1* treated_post_ia2* treated_post_ia* treated_post_ia*su* ///
										interact* treated treated_ia*se* treated_ia1* treated_ia2* treated_ia* treated_ia*su*)
						}	
				if $ownfirm == 1 & $data_int == 6 {
	 					outreg2 [eqn_2 eqn_4 eqn_5 eqn_6 eqn_7 eqn_8 eqn_12] ///
						using "`d'_`sose_type'_age_shortownf_noIQ_stem_$data.xls", ///
						replace keep(treated_pre treated_pre_ia*se* treated_pre_ia1* treated_pre_ia2* treated_pre_ia* treated_pre_ia*su*  ///
										treated_post treated_post_ia*se* treated_post_ia1* treated_post_ia2* treated_post_ia* treated_post_ia*su* ///
										interact* treated treated_ia*se* treated_ia1* treated_ia2* treated_ia* treated_ia*su*)
						}
				else if $ownfirm == 1 & $data_int == 5 	{
						outreg2 [eqn_2 eqn_4 eqn_5 eqn_6 eqn_7 eqn_8 eqn_12] ///
						using "`d'_`sose_type'_age_shortownf_noIQ_stem_$data.xls", /// 
						replace keep(treated_pre treated_pre_ia*se* treated_pre_ia1* treated_pre_ia2* treated_pre_ia* treated_pre_ia*su*  ///
										treated_post treated_post_ia*se* treated_post_ia1* treated_post_ia2* treated_post_ia* treated_post_ia*su* ///
										interact* treated treated_ia*se* treated_ia1* treated_ia2* treated_ia* treated_ia*su*)
				if $ownfirm == 1 & $data_int == 6 {
	 					outreg2 [eqn_top32 eqn_top33 eqn_top34 eqn_top35 eqn_top36 eqn_top37 eqn_top38] ///
						using "`d'_`sose_type'_age_top3ownf_noIQ_stem_$data.xls", ///
						replace keep(treated_pre treated_pre_ia*se* treated_pre_ia1* treated_pre_ia2* treated_pre_ia* treated_pre_ia*su*  ///
										treated_post treated_post_ia*se* treated_post_ia1* treated_post_ia2* treated_post_ia* treated_post_ia*su* ///
										interact* treated treated_ia*se* treated_ia1* treated_ia2* treated_ia* treated_ia*su*)
						}
				else if $ownfirm == 1 & $data_int == 5 	{
						outreg2 [eqn_top32 eqn_top33 eqn_top34 eqn_top35 eqn_top36 eqn_top37 eqn_top38] ///
						using "`d'_`sose_type'_age_top3ownf_noIQ_stem_$data.xls", ///
						replace keep(treated_pre treated_pre_ia*se* treated_pre_ia1* treated_pre_ia2* treated_pre_ia* treated_pre_ia*su*  ///
										treated_post treated_post_ia*se* treated_post_ia1* treated_post_ia2* treated_post_ia* treated_post_ia*su* ///
										interact* treated treated_ia*se* treated_ia1* treated_ia2* treated_ia* treated_ia*su*)
						}			
					*/
					}
				
				/*		
				outreg2 [eqn_long_1 eqn_long_2] using "`d'_`sose_type'_horser_long$data.xls", replace ///
						keep(treated treated_cow  inv_l_* inv_f_* inv_fl_*)
				*/
				}
		drop my_group		
	
		}
	local datacounter 	= `datacounter' + 1	
	}
*log close
	
	
exit
exit
exit	
	